//
//  TableViewController.m
//  Net2
//
//  Created by student on 16/3/28.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "TableViewController.h"
#import <UIImageView+WebCache.h>
#import "Video.h"
@interface TableViewController ()
@property (nonatomic,strong) NSMutableArray *list;
@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIRefreshControl *c=[[UIRefreshControl alloc]init];
    c.attributedTitle=[[NSAttributedString alloc]initWithString:@"获取列表中"];
    [c addTarget:self action:@selector(woshishuaige) forControlEvents:UIControlEventValueChanged];
    self.refreshControl=c;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)woshishuaige
{
    NSString *str=@"http://localhost:8080/MJServer/video?method=get&type=JSON";
    NSURL *url=[NSURL URLWithString:str];
    NSURLRequest *urlrequest=[NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:urlrequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if(connectionError==nil)
        {
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            NSArray *arr=dict[@"videos"];
            self.list=[NSMutableArray array];
            for(NSDictionary *d in arr)
            {
                Video *v=[Video videoWithDict:d];
                [self.list addObject:v];
            }
            [self.tableView reloadData];
        }
        else
        {
           
        }
        NSLog(@"%@",self.list);
        [self.refreshControl endRefreshing];
    }];
}
#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.list.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    Video *v=self.list[indexPath.row];
   
    cell.textLabel.text=v.name;
    cell.detailTextLabel.text=[NSString stringWithFormat:@"时长:%i",v.length];
    NSString *image=[NSString stringWithFormat:@"http://localhost:8080/MJServer/%@",v.image];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:image]placeholderImage:[UIImage imageNamed:@"minion_01"]];
    
    return cell;
}


@end
